import datetime
from django.db import models
from django.utils import timezone
from django.urls  import reverse

# Create your models here.
class ShopManager(models.Manager):
    def get_queryset(self):
        return super(ShopManager,self).get_queryset().filter(shop_name='饭团商店')


class Shop(models.Model):
    """
    商店
    """
    shop_name = models.CharField('商店名称',max_length=200)
    def __str__(self):
        return self.shop_name

    class Meta:
        verbose_name = '店铺'
        verbose_name_plural = '店铺管理'

class Good(models.Model):
    good_code=models.CharField('条形码',default="0",max_length=200)
    good_name = models.CharField('商品名称',max_length=200)
    good_price = models.IntegerField('价格',default=1)
    # good_num = models.IntegerField('库存',default=1)
    GOOD_TYPE_LIST = ((1, '主食'), (2, '副食'),(3,'零食'))
    good_type = models.IntegerField('类型',choices=GOOD_TYPE_LIST, default=1)

    def __str__(self):
        return self.good_name
    class Meta:
        verbose_name = '商品'
        verbose_name_plural = '商品管理'

class Order(models.Model):
    order_code = models.CharField('订单号',default="0",max_length=200)
    shop = models.ForeignKey(Shop, on_delete=models.CASCADE,verbose_name='商家')
    created_date=models.DateTimeField('订单时间',default=timezone.now())

    def __str__(self):
        return str(self.order_code)
    def was_created_recently(self):
        return self.created_date >= timezone.now() - datetime.timedelta(days=1)

    was_created_recently.admin_order_field = 'created_date'
    was_created_recently.boolean = True
    was_created_recently.short_description = '新订单？'

    def get_absolute_url(self):
        return reverse('polls/order_detail',
                       args=[self.created_date.year,
                             self.created_date.strftime('%m'),
                             self.created_date.strftime('%d'),
                             ])

    class Meta:
        verbose_name = '订单'
        verbose_name_plural = '订单管理'



class Good_of_Order(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    good = models.ForeignKey(Good, on_delete=models.CASCADE,verbose_name='商品')
    good_num = models.IntegerField('商品数量',default=1)
    def __str__(self):
        return ""
    class Meta:
        verbose_name = '订单商品'
        verbose_name_plural = '订单商品列表'